
//
// XamlDesignView.cs
//
// Author:
//   Olivier Dufour
//
// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;

using MonoDevelop.DesignerSupport.Toolbox;
using MonoDevelop.Projects;
using MonoDevelop.Core.Serialization;
using Stetic;
using MonoDevelop.Core;
using MonoDevelop.Ide.Gui;
using MonoDevelop.XamlDesigner.Model;
using MonoDevelop.XamlDesigner.Controller;
using System.Resources;
using System.Reflection;
using MonoDevelop.Core.Gui;
using System.Windows.Controls;
using MonoDevelop.XamlDesigner.View;

namespace MonoDevelop.XamlDesigner
{
	public class ToolboxProvider: IToolboxDynamicProvider, IToolboxDefaultProvider
	{
		internal static ToolboxProvider Instance;
		
		public ToolboxProvider ()
		{
			Instance = this;
            drawingModeItems = new List<ItemToolboxNode>(10);
            drawingModeItems.Add(CreateItem("Selection", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.selection-tool.png"), typeof(SelectionTool), "Draw"));
            drawingModeItems.Add(CreateItem("Rectangle", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.rectangle-tool.png"), typeof(RectangleCreationTool), "Draw"));
            drawingModeItems.Add(CreateItem("Square", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.square-tool.png"), typeof(SquareCreationTool), "Draw"));
            drawingModeItems.Add(CreateItem("Circle", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.circle-tool.png"), typeof(CircleCreationTool), "Draw"));
            drawingModeItems.Add(CreateItem("Ellipse", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.ellipse-tool.png"), typeof(EllipseCreationTool), "Draw"));
            drawingModeItems.Add(CreateItem("Path", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.path-tool.png"), typeof(PathCreationTool), "Draw"));
            drawingModeItems.Add(CreateItem("Line", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.line-tool.png"), typeof(LineCreationTool), "Draw"));
            drawingModeItems.Add(CreateItem("Polyline", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.polyline-tool.png"), typeof(PolyLineCreationTool), "Draw"));
            drawingModeItems.Add(CreateItem("Image", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.image-tool.png"), typeof(ImageCreationTool), "Draw"));
            drawingModeItems.Add(CreateItem("Pen", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.pen-tool.png"), typeof(PenCreationTool), "Draw"));
            
            designerModeItems = new List<ItemToolboxNode>(24);
            //container
            designerModeItems.Add(CreateItem("Grid", ImageService.GetPixbuf ("md-project" ), typeof(Grid), "Containers"));
            designerModeItems.Add(CreateItem("UserControl", ImageService.GetPixbuf ("md-project" ), typeof(UserControl), "Containers"));
            designerModeItems.Add(CreateItem("Canvas", ImageService.GetPixbuf ("md-project" ), typeof(Canvas), "Containers"));
            designerModeItems.Add(CreateItem("StackPanel", ImageService.GetPixbuf ("md-project" ), typeof(StackPanel), "Containers"));
            //designerModeItems.Add(CreateItem("VirtualizingStackPanel", ImageService.GetPixbuf ("md-project" ), typeof(System.Windows.Controls.VirtualizingStackPanel), "Containers"));
            //designerModeItems.Add(CreateItem("TabPanel", ImageService.GetPixbuf ("md-project" ), typeof(System.Windows.Controls.Primitives.TabPanel), "Containers"));
            
            //controls
            //designerModeItems.Add(CreateItem("Text", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.text-tool.png"), typeof(TextBlockCreationTool), "Shapes"));
            designerModeItems.Add(CreateItem("TextBlock", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.text-tool.png"), typeof(TextBlock), "Controls"));
            designerModeItems.Add(CreateItem("Button", ImageService.GetPixbuf ("md-project" ), typeof(System.Windows.Controls.Button), "Controls"));
            designerModeItems.Add(CreateItem("CheckBox", ImageService.GetPixbuf ("md-project" ), typeof(CheckBox), "Controls"));
            designerModeItems.Add(CreateItem("ComboBox", ImageService.GetPixbuf ("md-project" ), typeof(System.Windows.Controls.ComboBox), "Controls"));
            designerModeItems.Add(CreateItem("HyperlinkButton", ImageService.GetPixbuf ("md-project" ), typeof(HyperlinkButton), "Controls"));
            designerModeItems.Add(CreateItem("Image", ImageService.GetPixbuf ("md-project" ), typeof(System.Windows.Controls.Image), "Controls"));
            designerModeItems.Add(CreateItem("ListBox", ImageService.GetPixbuf ("md-project" ), typeof(ListBox), "Controls"));
            designerModeItems.Add(CreateItem("MultiScaleImage", ImageService.GetPixbuf ("md-project" ), typeof(MultiScaleImage), "Controls"));
            designerModeItems.Add(CreateItem("PasswordBox", ImageService.GetPixbuf ("md-project" ), typeof(PasswordBox), "Controls"));
            designerModeItems.Add(CreateItem("ProgressBar", ImageService.GetPixbuf ("md-project" ), typeof(System.Windows.Controls.ProgressBar), "Controls"));
            designerModeItems.Add(CreateItem("RadioButton", ImageService.GetPixbuf ("md-project" ), typeof(System.Windows.Controls.RadioButton), "Controls"));
            designerModeItems.Add(CreateItem("TextBox", ImageService.GetPixbuf ("md-project" ), typeof(TextBox), "Controls"));
            
            //shape
            designerModeItems.Add(CreateItem("Rectangle", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.rectangle-tool.png"), typeof(System.Windows.Shapes.Rectangle), "Shapes"));
            designerModeItems.Add(CreateItem("Ellipse", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.ellipse-tool.png"), typeof(System.Windows.Shapes.Ellipse), "Shapes"));
            designerModeItems.Add(CreateItem("Path", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.path-tool.png"), typeof(System.Windows.Shapes.Path), "Shapes"));
            designerModeItems.Add(CreateItem("Line", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.line-tool.png"), typeof(System.Windows.Shapes.Line), "Shapes"));
            designerModeItems.Add(CreateItem("Polyline", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.polyline-tool.png"), typeof(System.Windows.Shapes.Polyline), "Shapes"));
            designerModeItems.Add(CreateItem("Polygon", Gdk.Pixbuf.LoadFromResource ("MonoDevelop.XamlDesigner.Icons.polyline-tool.png"), typeof(System.Windows.Shapes.Polygon), "Shapes"));
                        
            designerModeItems.Add(CreateItem("MediaElement", ImageService.GetPixbuf ("md-project" ), typeof(MediaElement), "Media"));
            //designerModeItems.Add(CreateItem("Page", ImageService.GetPixbuf ("md-project" ), typeof(System.Windows.Controls.Page), "Media"));
            
            //designerModeItems.Add(CreateItem("InkPresenter", ImageService.GetPixbuf ("md-project" ), typeof(InkPresenter), "Containers"));
            
            //WPF ONLY: I keep it if we make this designer for WPF too
            //designerModeItems.Add(CreateItem("WrapPanel", ImageService.GetPixbuf ("md-project" ), typeof(WrapPanel), "Containers"));
            //designerModeItems.Add(CreateItem("ToolBarPanel", ImageService.GetPixbuf ("md-project" ), typeof(ToolBarPanel), "Containers"));
            //designerModeItems.Add(CreateItem("UniformGrid", ImageService.GetPixbuf ("md-project" ), typeof(UniformGrid), "Containers"));
            //designerModeItems.Add(CreateItem("ToolBarOverflowPanel", ImageService.GetPixbuf ("md-project" ), typeof(System.Windows.Controls.Primitives.ToolBarOverflowPanel), "Containers"));
            //designerModeItems.Add(CreateItem("DockPanel", ImageService.GetPixbuf ("md-project" ), typeof(DockPanel), "Containers"));
            //designerModeItems.Add(CreateItem("Viewport3D", ImageService.GetPixbuf ("md-project" ), typeof(Viewport3D), "Media"));
                
		}
        private List<ItemToolboxNode> drawingModeItems;
        private List<ItemToolboxNode> designerModeItems;
        
        public virtual IEnumerable<ItemToolboxNode> GetDynamicItems (IToolboxConsumer consumer)
		{
            XamlDesignView xamlConsumer = consumer as XamlDesignView;
            if (xamlConsumer == null)
                return null;
            
            if (xamlConsumer.Controller.DrawingMode)
                return drawingModeItems;
            else
                return designerModeItems;
        }
        
		public IEnumerable<ItemToolboxNode> GetDefaultItems ()
		{
            return null;
		}
                      
		public virtual IEnumerable<string> GetDefaultFiles ()
		{
			return null;
		}

		public void NotifyItemsChanged ()
		{
			if (ItemsChanged != null)
				ItemsChanged (this, EventArgs.Empty);
		}
        
		public event EventHandler ItemsChanged;

		private static ToolboxItemFilterAttribute att = new ToolboxItemFilterAttribute ("Xaml Designer Tool", ToolboxItemFilterType.Require);
        
        
        private ItemToolboxNode CreateItem(string name, Gdk.Pixbuf icon, Type type, string category)
		{
            ItemToolboxNode node = new TypeToolboxNode (type);
            node.Name = name + " tool";
            node.Description = name + " Xaml Designer Tool";
            node.Icon = icon;
            node.ItemFilters.Add(att);
            node.Category = category;
            node.Icon = icon;
            return node;
		}
	}
}
